﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsSM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// SM4加密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string strdata = textBox2.Text;
            string Key = textBox1.Text;
            if (string.IsNullOrEmpty(strdata))
            {
                MessageBox.Show("加密内容不能为空");
            }
            else if (string.IsNullOrEmpty(Key))
            {
                MessageBox.Show("秘钥不能为空");
            }
            else
            {
                textBox3.Text = SMEncryption.SM4EncryptBase64_ECB(strdata, Key);
            }
           
        }


        private void button5_Click(object sender, EventArgs e)
        {
            string strdata = textBox2.Text;
            string Key = textBox1.Text;
            if (string.IsNullOrEmpty(strdata))
            {
                MessageBox.Show("加密内容不能为空");
            }
            else if (string.IsNullOrEmpty(Key))
            {
                MessageBox.Show("秘钥不能为空");
            }
            else
            {
                textBox3.Text = SMEncryption.SM4EncryptBase64_ECB(strdata, Key);
            }

        }

        /// <summary>
        /// sm4秘钥生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string SM4Key = Guid.NewGuid().ToString();
            textBox1.Text = SM4Key;
        }

        /// <summary>
        /// SM4解密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            string strdata = textBox3.Text;
            string Key = textBox1.Text;
            if (string.IsNullOrEmpty(strdata))
            {
                MessageBox.Show("解密内容不能为空");
            }
            else if (string.IsNullOrEmpty(Key))
            {
                MessageBox.Show("秘钥不能为空");
            }
            else
            {
                textBox2.Text = SMEncryption.SM4DecryptBase64_ECB(strdata, Key);
            }
        }

        /// <summary>
        /// SM2加密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //string strdata = textBox8.Text;
            string strdata = textBox1.Text;
            string pubKey = textBox4.Text;
            if (string.IsNullOrEmpty(strdata))
            {
                MessageBox.Show("SM4密钥不能为空");
            }
            else if (string.IsNullOrEmpty(pubKey))
            {
                MessageBox.Show("公钥不能为空");
            }
            else
            {
                textBox9.Text = SMEncryption.SM2EncryptBase64(strdata, pubKey);
            }
            
        }

        /// <summary>
        /// SM4解密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            string strdata = textBox8.Text;
            string Key = textBox1.Text;
            if (string.IsNullOrEmpty(strdata))
            {
                MessageBox.Show("解密内容不能为空");
            }
            else if (string.IsNullOrEmpty(Key))
            {
                MessageBox.Show("秘钥不能为空");
            }
            else
            {
                textBox6.Text = SMEncryption.SM4DecryptBase64_ECB(strdata, Key);
            }
        }
        /// <summary>
        /// SM2解密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click_1(object sender, EventArgs e)
        {
            string strdata = textBox9.Text;
            string priKey = textBox5.Text;
            if (string.IsNullOrEmpty(strdata))
            {
                MessageBox.Show("解密内容不能为空");
            }
            else if (string.IsNullOrEmpty(priKey))
            {
                MessageBox.Show("私钥不能为空");
            }
            else
            {
                textBox7.Text = SMEncryption.SM2DecryptBase64(strdata, priKey);
            }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
            {
                ((TextBox)sender).SelectAll();
            } 
        }

        private void textBox8_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
            {
                ((TextBox)sender).SelectAll();
            } 
        }

        private void textBox7_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
            {
                ((TextBox)sender).SelectAll();
            } 
        }

        private void textBox6_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
            {
                ((TextBox)sender).SelectAll();
            } 
        }

        private void textBox9_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
            {
                ((TextBox)sender).SelectAll();
            } 
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
            {
                ((TextBox)sender).SelectAll();
            } 
        }

        private void button7_Click(object sender, EventArgs e)
        {
          string[] strkey = SM2.GenerateKeyPair();

           string pubkey = strkey[0].ToUpper().ToString();
            string priKey = strkey[1].ToUpper().ToString();

            textBox4.Text = pubkey;
            textBox5.Text = priKey;
        }
     
    }
}
